<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="900"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->

    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="总企业数" field="total_enterprise_count" style=" margin-top: 21px;
">
        <a-input v-model="formData.total_enterprise_count" placeholder="请输入总企业数" />
      </a-form-item>
      <a-form-item label="规划面积" field="planned_area">
        <a-input v-model="formData.planned_area" placeholder="请输入规划面积（单位：km²）" />
      </a-form-item>
      <a-form-item label="已建面积" field="built_area">
        <a-input v-model="formData.built_area" placeholder="请输入已建面积（单位：km²）" />
      </a-form-item>
      <a-form-item label="产业工人" field="industrial_worker_count">
        <a-input v-model="formData.industrial_worker_count" placeholder="请输入产业工人数量" />
      </a-form-item>
      <a-form-item label="成交数量" field="transaction_volume">
        <a-input v-model="formData.transaction_volume" placeholder="请输入成交数量（单位：笔）" />
      </a-form-item>
      <a-form-item label="成交金额" field="transaction_amount">
        <a-input v-model="formData.transaction_amount" placeholder="请输入成交金额" />
      </a-form-item>
      <a-form-item label="四上企业数" field="high_tech_enterprise_count">
        <a-input v-model="formData.high_tech_enterprise_count" placeholder="请输入四上企业数" />
      </a-form-item>
      <a-form-item label="制造企业数" field="manufacturing_enterprise_count">
        <a-input v-model="formData.manufacturing_enterprise_count" placeholder="请输入制造企业数" />
      </a-form-item>
      <a-form-item label="国家高新技术企业数" field="national_high_tech_enterprise_count">
        <a-input v-model="formData.national_high_tech_enterprise_count" placeholder="请输入国家高新技术企业数" />
      </a-form-item>
      <a-form-item label="外资企业数" field="foreign_invested_enterprise_count">
        <a-input v-model="formData.foreign_invested_enterprise_count" placeholder="请输入外资企业数" />
      </a-form-item>
      <a-form-item label="其他企业数" field="other_enterprise_count">
        <a-input v-model="formData.other_enterprise_count" placeholder="请输入其他企业数" />
      </a-form-item>
      <a-form-item label="专精特新中小企业数" field="specialized_and_sophisticated_sme_count">
        <a-input v-model="formData.specialized_and_sophisticated_sme_count" placeholder="请输入专精特新中小企业数" />
      </a-form-item>
      <a-form-item label="专精特新小巨人企业数" field="specialized_and_sophisticated_little_count">
        <a-input v-model="formData.specialized_and_sophisticated_little_count" placeholder="请输入专精特新小巨人企业数" />
      </a-form-item>
      <a-form-item label="战略性中小企业数" field="innovative_sme_count">
        <a-input v-model="formData.innovative_sme_count" placeholder="请输入战略性中小企业企业数" />
      </a-form-item>
      <a-form-item label="市级单项冠军" field="municipal_singular_leader_count">
        <a-input v-model="formData.municipal_singular_leader_count" placeholder="请输入市级单项冠军数" />
      </a-form-item>
      <a-form-item label="雏鹰企业" field="gazelle_enterprise_count">
        <a-input v-model="formData.gazelle_enterprise_count" placeholder="请输入瞪羚企业数" />
      </a-form-item>
      <a-form-item label="国家级单项冠军" field="national_singular_leader_count">
        <a-input v-model="formData.national_singular_leader_count" placeholder="请输入国家级单项冠军数" />
      </a-form-item>
      <a-form-item label="瞪羚企业数" field="unicorn_enterprise_count">
        <a-input v-model="formData.unicorn_enterprise_count" placeholder="请输入独角兽企业数" />
      </a-form-item>
      <a-form-item label="上市公司数" field="listed_company_count">
        <a-input v-model="formData.listed_company_count" placeholder="请输入上市公司数" />
      </a-form-item>
      <a-form-item label="园区布局" field="park_brand">
        <sa-upload-image v-model="formData.park_brand_image" :limit="3" :multiple="false" />
      </a-form-item>
      <a-form-item label="园区发展" field="park_development_image">
        <sa-upload-image v-model="formData.park_development_image" :limit="3" :multiple="false" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/dashboard'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')

let title = computed(() => {
  return '数据大屏' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  id: null,
  total_enterprise_count: null,
  planned_area: '',
  built_area: '',
  industrial_worker_count: null,
  transaction_volume: '',
  transaction_amount: '',
  high_tech_enterprise_count: null,
  manufacturing_enterprise_count: null,
  national_high_tech_enterprise_count: null,
  foreign_invested_enterprise_count: null,
  other_enterprise_count: null,
  specialized_and_sophisticated_sme_count: null,
  specialized_and_sophisticated_little_count: null,
  innovative_sme_count: null,
  municipal_singular_leader_count: null,
  gazelle_enterprise_count: null,
  national_singular_leader_count: null,
  unicorn_enterprise_count: null,
  listed_company_count: null,
  park_brand_image: '',
  park_development_image: '',
})

// 验证规则
// 统一验证规则
const rules = {
  total_enterprise_count: [{ required: true, message: '总企业数必需填写' }],
  planned_area: [{ required: true, message: '规划面积必需填写' }],
  built_area: [{ required: true, message: '已建面积必需填写' }],
  industrial_worker_count: [{ required: true, message: '产业工人数量必需填写' }],
  transaction_volume: [{ required: true, message: '成交数量必需填写' }],
  transaction_amount: [{ required: true, message: '成交金额必需填写' }],
  high_tech_enterprise_count: [{ required: true, message: '四上企业数必需填写' }],
  manufacturing_enterprise_count: [{ required: true, message: '制造企业数必需填写' }],
  national_high_tech_enterprise_count: [{ required: true, message: '国家高新技术企业数必需填写' }],
  foreign_invested_enterprise_count: [{ required: true, message: '外资企业数必需填写' }],
  other_enterprise_count: [{ required: true, message: '其他企业数必需填写' }],
  specialized_and_sophisticated_sme_count: [{ required: true, message: '专精特新中小企业数必需填写' }],
  specialized_and_sophisticated_little_count: [{ required: true, message: '专精特新小巨人企业数必需填写' }],
  innovative_sme_count: [{ required: true, message: '战略性中小企业必需填写' }],
  municipal_singular_leader_count: [{ required: true, message: '市级单项冠军数必需填写' }],
  gazelle_enterprise_count: [{ required: true, message: '雏鹰企业数必需填写' }],
  national_singular_leader_count: [{ required: true, message: '国家级单项冠军数必需填写' }],
  unicorn_enterprise_count: [{ required: true, message: '瞪鹰企业数必需填写' }],
  listed_company_count: [{ required: true, message: '上市公司数必需填写' }],
  park_brand_image: [{ required: true, message: '园区布局图必需上传' }],
  park_development_image: [{ required: true, message: '园区发展图必需上传' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
